import { _decorator, Component, Node, sys } from 'cc';
import { bannerwxwx } from './bannerwxwx';
const { ccclass, property } = _decorator;
import { BYTEDANCE, WECHAT } from 'cc/env';
@ccclass('WxSdkManager')
export class WxSdkManager {

    // 原生格子: 纵向（默认后台80%单个子）有2个的
    private static customCol33Id: string = bannerwxwx.customCol33Id
    private static customCol33Ad = null






    // 初始化纵向格子
    public static initCustomCol33Ad() {

        if (WECHAT) {
            if (this.customCol33Id == '') {
                console.log('【流量主】请配置3个纵向格子广告ID')
                return
            }
            let winSize = wx.getSystemInfoSync();
            if (this.customCol33Ad == null) {
                console.log('3个纵向格子广告成功');
                this.customCol33Ad = wx.createCustomAd({
                    adUnitId: this.customCol33Id,
                    adIntervals: 30,
                    style: {
                        width: 80,
                        left: winSize.screenWidth - 60,
                        top: winSize.screenHeight / 2 - 120,
                        fixed: 0
                    }
                })
                this.customCol33Ad.onError((err: any) => {
                    console.error('【流量主纵向3个格子】初始化有误')
                });
            }
        }

    }
    public static toggleCustomCol33Ad(isShow: boolean = true) {

        if (WECHAT) {
            if (this.customCol33Ad) isShow ? this.customCol33Ad.show() : this.customCol33Ad.hide();
        }

    }



     /**
     *  推荐组件参考代码
     *  核心由 pageManager实例 + openlink值 决定活动，开发者可根据下方代码自行适配
     */
     public static  recommendPageManager = null;


     /**
      *  游戏内提前加载推荐组件数据
      */
     public static  loadRecommend() {
 
         if (sys.platform == sys.Platform.WECHAT_GAME) {
             if (!wx.createPageManager) {
                 console.log('当前基础库版本暂不支持。');
             }
             try {
                 this.recommendPageManager = wx.createPageManager();
                 this.recommendPageManager.load({
                     openlink: 'TWFRCqV5WeM2AkMXhKwJ03MhfPOieJfAsvXKUbWvQFQtLyyA5etMPabBehga950uzfZcH3Vi3QeEh41xRGEVFw', // 推荐组件OPENLINK常量，直接复制即可，无需理解含义
                 });
             } catch (e) {
                 console.error('加载推荐组件失败:', e);
                 // 可以在这里添加降级处理逻辑
             }
         }
     }
 
 
 
     /**
      *  拉起推荐组件
      */
     public static showRecommend() {
         try {
             if (sys.platform == sys.Platform.WECHAT_GAME) {
                 console.error('加载推荐组件:');
                 if (!this.recommendPageManager) {
                     this.loadRecommend();
                 }
                 this.recommendPageManager.show();
             }
         } catch (e) {
             console.error('加载推荐组件失败:', e);
             // 可以在这里添加降级处理逻辑
         }
     }

}


